package org.cy.shixun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.cy.shixun.dto.GoodsDto;
import org.cy.shixun.entity.Goods;
import org.cy.shixun.entity.GoodsSize;
import org.cy.shixun.mapper.GoodsMapper;
import org.cy.shixun.mapper.GoodsSizeMapper;
import org.cy.shixun.service.GoodsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author TrainingL
 * @since 2023-06-29
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsSizeMapper goodsSizeMapper;
    
    @Override
    public List<Goods> category(String category) {
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Goods::getCategoryName,category);
        List<Goods> goods = goodsMapper.selectList(queryWrapper);
        return goods;
    }

    @Override
    public GoodsDto item(int id) {
        Goods goods = goodsMapper.selectById(id);
        
        LambdaQueryWrapper<GoodsSize> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GoodsSize::getGoodsId,id);
        List<GoodsSize> goodsSizes = goodsSizeMapper.selectList(queryWrapper);
        
        GoodsDto goodsDto = new GoodsDto();
        BeanUtils.copyProperties(goods,goodsDto);

        List<String> collect = goodsSizes.stream().map(item -> {
            String size = item.getSize();
            return size;
        }).collect(Collectors.toList());
        goodsDto.setSize(collect);
        return goodsDto;
    }
}
